﻿using UnityEngine;
using System.Collections;
using UI;
using System.Collections.Generic;
using ProtoBuf.Message;

public class ContinueGiftGet : MonoBehaviour
{
    public SimpleGrid grid;
    public UIScrollView scrollView;
    public GameObject iconPrefab;

    public UIActivityContinueChargeGift uiActivity;

    ActivityItem curActivityItem;
    public void SetData(ActivityItem item)
    {
        this.curActivityItem = item;
    }

    public void Show()
    {
        gameObject.SetActive(true);
        Refresh();
    }

    public void Hide()
    {
        foreach (var obj in grid.childs)
        {
            Destroy(obj);
        }
        grid.childs.Clear();
        gameObject.SetActive(false);
    }

    void Refresh()
    {
        foreach (var res in curActivityItem.rewardList)
        {
            GameObject obj = GameObject.Instantiate(iconPrefab);
            obj.SetActive(true);
            obj.GetComponent<ContinueGiftGetItem>().Init(res);
            grid.AddChild(obj);
        }

    }

    public void OnClickGet()
    {
        CSContinuedRechargeReward msg = new CSContinuedRechargeReward();
        msg.activityId = uiActivity.activityId;
        msg.index = curActivityItem.index;
        msg.type = 0;
        msg.getResources.AddRange(curActivityItem.rewardList);
        NetAPI.SendHttp(OpDefine.CSContinuedRechargeReward, msg, GetAllRewardCallback);
    }

    void GetAllRewardCallback(NetWork.Packets.Packet packet, bool bSuccess)
    {
        if (bSuccess)
        {
            SCContinuedRechargeReward msg = packet.kBody as SCContinuedRechargeReward;
            StaticData.playerData.AddResultObj(msg.get);
            uiActivity.SetBigGiftStatus(msg.status);
            BoxManager.CreatePopupTextBox(TableTextBlo.GetText("get_success"));
            bool flag = true;
            foreach (var item in msg.itemList)
            {
                if (item.status == 1)
                {
                    flag = false;
                    break;
                }
            }
            if (flag && uiActivity.bigGiftBtn.activeSelf == false)
            {
                StaticData.redTips.SetPointState(RedPointTip.Red.continuedRecharge, 0);
                StaticData.redTips.ReduceActivitys();
            }

            uiActivity.HideGetGiftPanel();
        }
    }
  
}
